package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.util.ParameterUtil;

public class DisinfectionResultUpdateThread extends BasicThread {

	public interface DisinfectionResultUpdateThreadCallback {
		public void OnDisinfectionResultUpdateCompleted(boolean isNetworkConntected, String result
				, String dnsfResNo, String message);
		public void OnDisinfectionResultUpdatelError(ErrorItem errorItem);
	}

	private String url;
	private DisinfectionResultUpdateThreadCallback callback;
	private ArrayList<NameValuePair> post;
	
	/* 파라메터*/

	private String master, detail;
	public DisinfectionResultUpdateThread(Context context, DisinfectionResultUpdateThreadCallback callback
			, String master, String detail, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.appStatus = (AppStatus) context.getApplicationContext();
		this.master = master;
		this.detail = detail;
	}

	@Override
	public void run() {
		Map<String, String> map = new HashMap<String, String>();
		String param = "";
		
		map.put(Const.RESULTMASTER, master);
		map.put(Const.RESULTDETAIL, detail);
		param = parameterReturn(Const.REQ015, (HashMap<String, String>) map);
		
		serviceBroker(param, context);
	}

	@Override
	public void settingObject() {
		// 통신불통상태이면
		if(!appStatus.utilm.isNetworkConnected(context)) {
			callback.OnDisinfectionResultUpdateCompleted(false, "", "","통신 에러");
			return;
		}
		
		try {
			json = new JSONObject(returnJson);
			if(json == null) {	// json 값이 null 일때
				callback.OnDisinfectionResultUpdatelError(null);
				return;
			}
			int result = json.getInt(Const.RESULT);
			String data = json.getString(Const.DATA);
			if(result == 0){	// result 값이 0일 경우
				ErrorItem errorItem = new ErrorItem();
				errorItem.setrDesc(data);

				callback.OnDisinfectionResultUpdatelError(errorItem);
			}else{	// result 값이 0이 아닐 경우
				Log.i("data","data =" + data);
				String dnsfResNo = json.getString(Const.DSNFRESNO);
				Log.i("dnsfResNo","dnsfResNo =" + dnsfResNo);
				callback.OnDisinfectionResultUpdateCompleted(true, data, dnsfResNo, mResult);
			}
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	}
}
